package com.elite.groovy.kh;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @projectName: ngsapitask-davco
 * @package: com.elite.groovy.kh
 * @className: excelUtils
 * @description: TODO
 * @author: LiJT
 * @date: 2023-11-15 17:38
 * @version: 1.0
 */
public class excelUtils {
    public static void main(String[] args) throws IOException {
        out2007();
    }

    public static void out2007() throws IOException {
        // 开始时间
        long start = System.currentTimeMillis();
        String path = "E:\\";
        // 1.创建一个工作簿。03
        Workbook workbook = new SXSSFWorkbook(5000);

        CellStyle cellStyle = workbook.createCellStyle();
        // 设置背景色
        cellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        // 设置字体
        Font font = workbook.createFont();
        font.setItalic(false);
        font.setStrikeout(false);
        font.setBold(true);
        cellStyle.setFont(font);

        // 2.创建一个工作表
        Sheet sheet = workbook.createSheet("统计表");
        sheet.createFreezePane(0, 1, 0, 1);


        // 3.创建行。
        for (int rowNum = 0; rowNum < 65537; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                if (rowNum == 0) {
                    cell.setCellStyle(cellStyle);
                }
                cell.setCellValue(rowNum + "," + cellNum);
            }
        }
        // 5.生成一张表。03版本的工作簿是以.xlsx结尾
        FileOutputStream fileOutputStream = new FileOutputStream(path + KHUtil.getRandomString(6) + "07BigDataUpGrade.xlsx");
        // 输出
        workbook.write(fileOutputStream);
        // 6.关闭流
        fileOutputStream.close();
        // 7.清除临时文件
        ((SXSSFWorkbook) workbook).dispose();
        System.out.println("07大数据量表优化后生成成功！");
        // 结束时间
        long end = System.currentTimeMillis();
        System.out.println("用时：" + ((end - start) / 1000) + "秒");
    }

}
